package com.pgmmers.radar.dal.model.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pgmmers.radar.dal.bean.DataListQuery;
import com.pgmmers.radar.dal.bean.DataListRecordQuery;
import com.pgmmers.radar.dal.bean.PageResult;
import com.pgmmers.radar.dal.model.DataListDal;
import com.pgmmers.radar.mapper.DataListMetaMapper;
import com.pgmmers.radar.mapper.DataListRecordMapper;
import com.pgmmers.radar.mapper.DataListsMapper;
import com.pgmmers.radar.mapstruct.DataListMetaMapping;
import com.pgmmers.radar.mapstruct.DataListRecordMapping;
import com.pgmmers.radar.mapstruct.DataListsMapping;
import com.pgmmers.radar.model.DataListMetaPO;
import com.pgmmers.radar.model.DataListRecordPO;
import com.pgmmers.radar.model.DataListsPO;
import com.pgmmers.radar.util.BaseUtils;
import com.pgmmers.radar.vo.model.DataListMetaVO;
import com.pgmmers.radar.vo.model.DataListRecordVO;
import com.pgmmers.radar.vo.model.DataListsVO;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;


@Service
public class DataListDalImpl implements DataListDal {

    public static Logger logger = LoggerFactory.getLogger(DataListDalImpl.class);

    @Autowired
    private DataListsMapper dataListMapper;
    @Resource
    private DataListsMapping dataListsMapping;
    @Autowired
    private DataListRecordMapper dataListRecordMapper;
    @Resource
    private DataListRecordMapping dataListRecordMapping;
    @Autowired
    private DataListMetaMapper dataListMetaMapper;
    @Resource
    private DataListMetaMapping dataListMetaMapping;

    @Override
    public List<DataListMetaVO> listDataMeta(Long dataListId) {
        logger.info("listDataRecord,{}", dataListId);
        List<DataListMetaPO> list = dataListMetaMapper.selectList(
                Wrappers.lambdaQuery(DataListMetaPO.class).eq(DataListMetaPO::getDataListId, dataListId)
        );
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        return dataListMetaMapping.sourceToTarget(list);
    }

    @Override
    public List<DataListRecordVO> listDataRecord(Long dataListId) {
        logger.info("listDataRecord,{}", dataListId);
        List<DataListRecordPO> list = dataListRecordMapper.selectList(
                Wrappers.lambdaQuery(DataListRecordPO.class).eq(DataListRecordPO::getDataListId, dataListId)
        );
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        return dataListRecordMapping.sourceToTarget(list);
    }

    @Override
    public DataListMetaVO getListMetaById(Long metaId) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<DataListsVO> listDataLists(Long modelId, Integer status) {
        logger.info("listDataLists,{},{}", modelId, status);

        LambdaQueryWrapper<DataListsPO> wrapper = Wrappers.lambdaQuery(DataListsPO.class);
        wrapper.eq(DataListsPO::getModelId, modelId);
        if (status != null) {
            wrapper.eq(DataListsPO::getStatus, status);
        }
        List<DataListsPO> list = dataListMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        return dataListsMapping.sourceToTarget(list);
    }

    @Override
    public DataListsVO get(Long id) {
        DataListsPO dataListsPO = dataListMapper.selectById(id);
        if (dataListsPO != null) {
            return dataListsMapping.sourceToTarget(dataListsPO);
        }
        return null;
    }

    @Override
    public List<DataListsVO> list(Long modelId) {
        LambdaQueryWrapper<DataListsPO> wrapper = Wrappers.lambdaQuery(DataListsPO.class);
        wrapper.in(DataListsPO::getModelId, Arrays.asList(modelId, 1L));
        List<DataListsPO> list = dataListMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        return dataListsMapping.sourceToTarget(list);
    }

    @Override
    public PageResult<DataListsVO> query(DataListQuery query) {
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        LambdaQueryWrapper<DataListsPO> wrapper = Wrappers.lambdaQuery(DataListsPO.class);
        wrapper.eq(DataListsPO::getModelId, query.getModelId());
        if (!StringUtils.isEmpty(query.getName())) {
            wrapper.like(DataListsPO::getName, BaseUtils.buildLike(query.getName()));
        }
        if (!StringUtils.isEmpty(query.getListType())) {
            wrapper.eq(DataListsPO::getListType, query.getListType());
        }
        if (query.getStatus() != null) {
            wrapper.eq(DataListsPO::getStatus, query.getStatus());
        }
        List<DataListsPO> list = dataListMapper.selectList(wrapper);
        Page<DataListsPO> page = (Page<DataListsPO>) list;
        List<DataListsVO> listVO;
        if (CollectionUtils.isEmpty(list)) {
            listVO = new ArrayList<>();
        } else {
            listVO = dataListsMapping.sourceToTarget(list);
        }
        return new PageResult<>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), listVO);
    }

    @Override
    public int save(DataListsVO datalist) {
        DataListsPO dataListsPO = dataListsMapping.targetToSource(datalist);
        Date sysDate = new Date();
        int count = 0;
        if (datalist.getId() == null) {
            dataListsPO.setCreateTime(sysDate);
            dataListsPO.setUpdateTime(sysDate);
            count = dataListMapper.insert(dataListsPO);
            datalist.setId(dataListsPO.getId());// 返回id
        } else {
            dataListsPO.setUpdateTime(sysDate);
            count = dataListMapper.updateById(dataListsPO);
        }
        return count;
    }

    @Override
    public int delete(Long[] id) {
        int count = dataListMapper.delete(Wrappers.lambdaQuery(DataListsPO.class).in(DataListsPO::getId, Arrays.asList(id)));
        // TODO 删除关联子表
        return count;
    }

    @Override
    public DataListMetaVO getMeta(Long id) {
        DataListMetaPO dataListMetaPO = dataListMetaMapper.selectById(id);
        if (dataListMetaPO != null) {
            return dataListMetaMapping.sourceToTarget(dataListMetaPO);
        }
        return null;
    }

    @Override
    public int saveMeta(DataListMetaVO dataListMeta) {
        DataListMetaPO dataListMetaPO = dataListMetaMapping.targetToSource(dataListMeta);
        Date sysDate = new Date();
        int count = 0;
        if (dataListMetaPO.getId() == null) {
            dataListMetaPO.setCreateTime(sysDate);
            dataListMetaPO.setUpdateTime(sysDate);
            count = dataListMetaMapper.insert(dataListMetaPO);
            dataListMeta.setId(dataListMetaPO.getId());// 返回id
        } else {
            dataListMetaPO.setUpdateTime(sysDate);
            count = dataListMetaMapper.updateById(dataListMetaPO);
        }
        return count;
    }

    @Override
    public int deleteMeta(List<Long> listId) {
        int count = dataListMetaMapper.delete(Wrappers.lambdaQuery(DataListMetaPO.class).in(DataListMetaPO::getId, listId));
        // TODO 删除关联子表
        return count;
    }

    @Override
    public DataListRecordVO getRecord(Long id) {
        DataListRecordPO dataListRecordPO = dataListRecordMapper.selectById(id);
        if (dataListRecordPO != null) {
            return dataListRecordMapping.sourceToTarget(dataListRecordPO);
        }
        return null;
    }

    @Override
    public PageResult<DataListRecordVO> queryRecord(DataListRecordQuery query) {
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<DataListRecordPO> list = dataListRecordMapper.selectList(
                Wrappers.lambdaQuery(DataListRecordPO.class)
                        .eq(DataListRecordPO::getDataListId, query.getDataListId())
        );
        Page<DataListRecordPO> page = (Page<DataListRecordPO>) list;
        List<DataListRecordVO> listVO;
        if (CollectionUtils.isEmpty(list)) {
            listVO = new ArrayList<>();
        } else {
            listVO = dataListRecordMapping.sourceToTarget(list);
        }
        PageResult<DataListRecordVO> pageResult = new PageResult<>(page.getPageNum(),
                page.getPageSize(), (int) page.getTotal(), listVO);
        return pageResult;
    }

    @Override
    public int saveRecord(DataListRecordVO dataListRecord) {
        DataListRecordPO dataListRecordPO = dataListRecordMapping.targetToSource(dataListRecord);
        Date sysDate = new Date();
        int count = 0;
        if (dataListRecordPO.getId() == null) {
            dataListRecordPO.setCreateTime(sysDate);
            dataListRecordPO.setUpdateTime(sysDate);
            count = dataListRecordMapper.insert(dataListRecordPO);
            dataListRecord.setId(dataListRecordPO.getId());// 返回id
        } else {
            dataListRecordPO.setUpdateTime(sysDate);
            count = dataListRecordMapper.updateById(dataListRecordPO);
        }
        return count;
    }

    @Override
    public int deleteRecord(Long[] id) {
        int count = dataListRecordMapper.delete(Wrappers.lambdaQuery(DataListRecordPO.class).in(DataListRecordPO::getId, Arrays.asList(id)));
        // TODO 删除关联子表
        return count;
    }

    @Override
    public int deleteRecord(Long dataListId) {
        int count = dataListRecordMapper.delete(Wrappers.lambdaQuery(DataListRecordPO.class).in(DataListRecordPO::getDataListId, dataListId));
        // TODO 删除关联子表
        return count;
    }

}
